package com.zabra.zl.ui.verifyphone;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.data.SimpleBean;
import com.zabra.zl.ui.inputpwd.InputPwdActivity;
import com.zabra.zl.ui.updatepwd_islogin.UpdatePwdIsLoginActivity;
import com.zabra.zl.ui.verifyphone.contract.VerifyPhoneContract;
import com.zabra.zl.ui.verifyphone.presenter.VerifyPhonePresenter;
import com.zabra.zl.utils.CountDownTimerUtils;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.MaptoJsonUtils;
import com.zabra.zl.utils.SharedPreferencesUtil;
import com.zabra.zl.utils.ShowDialogUtils;
import com.zabra.zl.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VerifyPhoneActivity extends BaseMvpActivity<VerifyPhonePresenter> implements VerifyPhoneContract.View {


    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.bt_next)
    TextView btNext;
    @BindView(R.id.et_tel)
    EditText etTel;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_getcode)
    TextView tvGetcode;
    private Dialog dialog;
    private int type;
    private String strPhone;
    private Bundle bundle=new Bundle();
    @Override
    public int getLayoutId() {
        return R.layout.activity_verify_phone;
    }

    @Override
    public void initView() {
        tvTitle.setText("验证身份");
        type = getIntent().getExtras().getInt("type");
        mPresenter = new VerifyPhonePresenter();
        mPresenter.attachView(this);
    }

    @Override
    public void showLoading() {
        dialog = DialogUtils.showDialog_progressbar(this);
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onSuccess(SimpleBean bean, String token) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            SharedPreferencesUtil.getInstance(this).putSP("token", token);
            CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(tvGetcode, 60000, 1000);
            mCountDownTimerUtils.start();
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @Override
    public void onSuccess(SimpleBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            if (type == 1) {
                //修改登录密码
                bundle.putString("tel",strPhone);
                toClass(this,UpdatePwdIsLoginActivity.class,bundle);
            }if (type==3){
                toClass1(this,InputPwdActivity.class,bundle);
            }
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }


    @OnClick({R.id.back, R.id.bt_next, R.id.tv_getcode})
    public void onViewClicked(View view) {
        strPhone = etTel.getText().toString().trim();
        String strCOde = etCode.getText().toString().trim();
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.bt_next:
                if (TextUtils.isEmpty(strCOde)) {
                    ToastUtil.showToast(this, "请输入验证码");
                } else {
                    keyList.add("phone");
                    keyList.add("yzm");
                    valueList.add(strPhone);
                    valueList.add(strCOde);
                    mPresenter.getData(MaptoJsonUtils.Simple(keyList, valueList));
                    keyList.clear();
                    valueList.clear();
                }
                break;
            case R.id.tv_getcode:
                if (TextUtils.isEmpty(strPhone)) {
                    ToastUtil.showToast(this, "请输入正确电话号码");
                } else {
                    keyList.add("phone");
                    keyList.add("debtorId");
                    valueList.add(strPhone);
                    valueList.add(strUserid);
                    mPresenter.getVerCodeData(MaptoJsonUtils.Simple(keyList, valueList));
                    keyList.clear();
                    valueList.clear();
                }
                break;
        }
    }

}
